package cn.yzgy.yzgyoa.common.controller;

import cn.yzgy.yzgyoa.ucenter.entity.SysUser;
import cn.yzgy.yzgyoa.ucenter.entity.UserInfoDetail;
import cn.yzgy.yzgyoa.ucenter.service.impl.SysUserDetailServiceImpl;
import lombok.Getter;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 控制器基础类，封装了控制器中常用的对象和方法
 *
 * @author Yqsen
 * @version 0.0.1
 * @since 2019-8-16 10:44:03
 */
@SuppressWarnings("unused")
public class BaseController {
    @Getter
    @Resource
    private HttpServletRequest request;

    @Getter
    @Resource
    private HttpServletResponse response;

    @Autowired
    private SysUserDetailServiceImpl sysUserDetailService;

    /**
     * 获取当前方法名
     *
     * @return 当前方法名
     */
    protected String getCurrentMethodName() {
        return Thread.currentThread().getStackTrace()[2].getMethodName();
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 当前登录的用户信息
     */
    protected UserInfoDetail getCurrentUser(){
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        return sysUserDetailService.getUserDetailInfoByUid(user.getId());
    }

    /**
     * 获取当前会话中保存的openid
     *
     * @return openid
     */
    protected HttpSession getSession() {
        return request.getSession();
    }

    protected static Subject getSubject(){
        return SecurityUtils.getSubject();
    }
}
